<?php

namespace app\api\model\wanlshop;

use think\Model;
use traits\model\SoftDelete;
use function EasyWeChat\Kernel\Support\get_client_ip;
class GoodsSku extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'wanlshop_goods_sku';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
	
	
	// getDifferenceAttr
	public function getDifferenceAttr($value)
	{	
		return $value ? explode(',', $value) : [];
	}


	public function getPriceAttr($value,$data){
//        if(!isIpCn(get_client_ip())){
            //蒙古重新计算价格
//            if(isset($data['price']) && $data['price'] !== ''){
//               $mgRate = get_addon_config('wanlshop')['config']['mg_rate'];
//                $value = $data['price'] * $mgRate;
//            }
//        }
        if(isset($data['price']) && $data['price'] !== '' && isset($data['goods_id']) && $data['goods_id'] > 0){
            $shopId = Goods::where(['id'=>$data['goods_id']])->value('shop_id');
            $isChina = Shop::where(['id'=>$shopId])->value('is_china');
            if($isChina == 1 && isUrlRussian()){
                $mgRate = get_addon_config('wanlshop')['config']['mg_rate'];
                $value = $data['price'] * $mgRate;
            }
        }

        return $value;
    }


    public function getMarketPriceAttr($value,$data){
        if(isset($data['market_price']) && $data['market_price'] !== '' && isset($data['goods_id']) && $data['goods_id'] > 0){
            $shopId = Goods::where(['id'=>$data['goods_id']])->value('shop_id');
            $isChina = Shop::where(['id'=>$shopId])->value('is_china');
            if($isChina == 1 && isUrlRussian()){
                $mgRate = get_addon_config('wanlshop')['config']['mg_rate'];
                $value = $data['market_price'] * $mgRate;
            }
        }

        return $value;
    }
}
